package com.nutstudio.feitoys;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by Administrator on 2017/4/25 0025.
 */

public class MyWaveView extends View implements View.OnClickListener{

    public MyWaveView(Context context) {
        super(context);
    }

    public MyWaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyWaveView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint mPaint = new Paint();
        Path mPath = new Path();
        mPaint.setColor(Color.RED); //画笔颜色
        mPaint.setStrokeWidth(10); //画笔宽度
        mPaint.setStyle(Paint.Style.STROKE);
        int hPos = getHeight() / 2;
        int waveH=getHeight()/4;

            mPath.reset();
            mPath.moveTo(0, hPos);
            mPath.quadTo(getWidth() / 3 / 2, waveH,getWidth() / 3, hPos);
            canvas.drawPath(mPath, mPaint);
            canvas.drawPoint( getWidth() / 3 / 2, waveH, mPaint);

            mPath.reset();
            mPath.moveTo(getWidth() / 3, hPos);
            mPath.quadTo(getWidth() / 3+getWidth() / 3 / 2, getHeight()-waveH,getWidth() / 3*2, hPos);
            canvas.drawPath(mPath, mPaint);
            canvas.drawPoint(getWidth() / 3+getWidth() / 3 / 2, getHeight()-waveH, mPaint);
            //
            mPath.reset();
            mPath.moveTo(getWidth() / 3*2, hPos);
            mPath.quadTo(getWidth() / 3*2+getWidth() / 3 / 2,waveH,getWidth(), hPos);
            canvas.drawPath(mPath, mPaint);
            canvas.drawPoint(getWidth() / 3*2+getWidth() / 3 / 2, waveH, mPaint);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        setOnClickListener(this);
    }
    private ValueAnimator mValueAnimatior;
    @Override
    public void onClick(View view) {
        mValueAnimatior = ValueAnimator.ofInt(0, getWidth()/3);
        mValueAnimatior.setDuration(1000);
        mValueAnimatior.setInterpolator(new LinearInterpolator());
        mValueAnimatior.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimatior.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int offset = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        mValueAnimatior.start();
    }

    class MyPoint {
        private int x;

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        private int y;
    }
}
